<?php
date_default_timezone_set('Asia/Shanghai');

$script_php_dir = "/soooner/cam_upload_script/php/";

$LOG_DF = date('Ymd');
$file_log = "{$script_php_dir}log/{$LOG_DF}_upload_file.log";
$request_log = "{$script_php_dir}log/{$LOG_DF}_request.log";

$report_error_file_log = "{$script_php_dir}log/{$LOG_DF}_report_error_file.log";

include_once($script_php_dir."include/mcrypt.php");

##################xml config read#################
//$configXml = "/soooner/sns_flv/sns.xml";
//if(file_exists($configXml)){
//    $xml = simplexml_load_file($configXml);
//	
//	$Devid = $xml->header->switchsid;
//}
##################xml config read#################

$filepath = $argv[1];
$file = $argv[2];
$camid = $argv[3];
$shareid = $argv[4];
$f1_upload_url = $argv[5];
$token = $argv[6];
$vodtimelen = $argv[7];

clearstatcache();
$file_size = filesize($filepath."/".$file);

if(strstr($shareid,'_')){
	#上传文件
	$data = array(
		"file"  => "@".$filepath."/".$file
		);
	$f1_upload_url .= "?utype=road&eventid=$shareid&md5=".md5_file($filepath."/".$file)."&token=$token&size=$file_size&len=$vodtimelen";
}else{
	#上传文件
	$data = array(
		"camid"=>$camid,
		"shareid"=>$shareid,
		"file_md5"=>md5_file($filepath."/".$file),
		"token"=>$token,
		"file"  => "@".$filepath."/".$file
		);
}
echo $f1_upload_url;
var_dump($data);
$f1_result = json_decode(uploadByCURL($data,$f1_upload_url),true);
var_dump($f1_result);
#返回：{ “result”:0 ,” “url”:”http://111.111.2.2/otherpath/camid/file” }
if($f1_result){
	if($f1_result["result"]==0){
		/*if($ext=="jpg"){
			$success["images"][] = array("file"=>$file,"url"=>$f1_result['url']);
		}else if($ext=="flv"||$ext=="idx"){
			$success["videofiles"][] = array("file"=>$file,"url"=>$f1_result['url']);
		}*/

		#echo $f1_result['url']."\n";

		echo "Uploaded : $filepath/".$file."\n";
		#echo "URL :".$f1_result['url']."\n";
		rename($filepath."/".$file,$filepath."/".$file.".old");
	}else if($f1_result["result"]==1){
		if($f1_result["msg"]=='no record effected'){
			echo "Warning : $filepath/".$file."\n";
			rename($filepath."/".$file,$filepath."/".$file.".old");
		}
	}
}

#解锁
dir_unlock($filepath);

########生成日志
#$logFile = fopen($file_log,"a");
#fwrite($logFile,"{$filepath}/{$file};\n");
#fclose($logFile);
#######


/*
if(isset($success)){

	################# D-3 Report #################

	
	$request_url = "http://i.cam.soooner.com/filestatus.do";
	$success['camid'] = $camid;
	$success = json_encode($success);
	$d3_result = json_decode(uploadByCURL($success,$request_url),true);
	if($d3_result["result"]==0){
		unset($success);
		echo "Reported Success...:$filepath/$file\n";
	}else{
		$reportErrorFile = fopen($report_error_file_log,"a");
		fwrite($reportErrorFile,"{$filepath}/{$file};\n");
		fclose($reportErrorFile);
	}

	################# D-3 #################

}*/


//公共-CURL-POST
function uploadByCURL($post_data,$post_url){
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0 );
	curl_setopt($curl, CURLOPT_TIMEOUT, 600 );
	curl_setopt($curl, CURLOPT_URL, $post_url);
	curl_setopt($curl, CURLOPT_LOW_SPEED_LIMIT, 102400);
	curl_setopt($curl, CURLOPT_POST, 1 );
	curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($curl,CURLOPT_USERAGENT,"Mozilla/4.0");
	$result = curl_exec($curl);
	$error = curl_error($curl);
close
	########生成文件上传请求日志
	global $request_log;
	$logRequest = fopen($request_log,"a");
	fwrite($logRequest,date("Y-m-d H:i:s")." $post_url ".json_encode($post_data)." $result $error"."\n");
	fclose($logRequest);
	#######

	return $error ? $error : $result;
}


//目录锁定
function dir_lock($path){
	if(file_exists($path)){
		$path .= "/lock";
		$fp = fopen($path,"a");
		fwrite($fp,date("Y-m-d H:i:s")."\n");
		fclose($fp);
	}	
}

//目录解锁
function dir_unlock($path){
	if(file_exists($path)){
		$path .= "/lock";
		if(file_exists($path)){
			unlink($path);	
		}		
	}
}

//是否锁定
function dir_islock($path){
	if(file_exists($path)){
		$path .= "/lock";
		return file_exists($path);
	}
}